package com.qyl.deguru.controller.customer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.qyl.deguru.common.PageConstant;
import com.qyl.deguru.dto.Shop;
import com.qyl.deguru.service.ShopService;
import com.qyl.deguru.service.impl.ShopServiceImple;

/**
 * 购买控制类
 * */
@Controller
@RequestMapping("/customer/buy")
public class BuyController {
	
	ShopService shopService = new ShopServiceImple();
	//添加到购物车
	@RequestMapping("/addShopCar")
	@ResponseBody
	public Map<String, String> addShopCar(int shopsId, int buyNum,
			HttpSession session) {
		Map<String, String> map = new HashMap<>();
		// 获取购物车
		Map<Integer, Integer> shopCar = (Map<Integer, Integer>) session
				.getAttribute(PageConstant.SHOP_CAR);
		try {
			// 判断购物车是否为空
			if (shopCar == null) {
				shopCar = new LinkedHashMap<>();
			}
			// 判断购物车是否存在该商品
			if (shopCar.containsKey(shopsId)) {
				// 获取购商品数量,叠加数量
				int num = shopCar.get(shopsId);
				shopCar.put(shopsId, buyNum + num);
			} else {
				shopCar.put(shopsId, buyNum);
			}
			// 存入缓存
			session.setAttribute(PageConstant.SHOP_CAR, shopCar);
			map.put("status", "success");
		} catch (Exception e) {
			map.put("status", "fail");
		}
		return map;
	}

	// 显示购物车列表
	@RequestMapping("/showShopCar")
	public String showShopCar(HttpSession session, ModelMap map) {
		// 获取购物车
		Map<Integer, Integer> shopCar = (Map<Integer, Integer>) session
				.getAttribute(PageConstant.SHOP_CAR);
		List<Shop> shopsList = null;
		if (shopCar != null) {
			shopsList = new ArrayList<>();
			Set<Entry<Integer, Integer>> cars = shopCar.entrySet();
			for (Map.Entry<Integer, Integer> car : cars) {
				// 获取商品id
				int shopsId = car.getKey();
				// 根据获取商品
				Shop shops=shopService.getShopById(shopsId);
				// 获取商品数量
				int buyCount = car.getValue();
				shops.setBuyNum(buyCount);
				// 把goods商品存到list中
				shopsList.add(shops);
			}
		}
		map.addAttribute("shopsList", shopsList);
		return "shop_cart";
	}
	//发送ajax请求，点击+和-
	@RequestMapping("/updateShopCar")
	@ResponseBody
	public Map<String, String> updateShopCar(HttpSession session,int shopsId,int num){
		Map<String, String> map=new HashMap<>();
		Map<Integer, Integer> shopcars=(Map<Integer,Integer>) session.getAttribute(PageConstant.SHOP_CAR);
		shopcars.put(shopsId, num);
		map.put("status", "success");
		return map;
	}
//	//展示购物车列表
//	@RequestMapping("/getAjaxCount")
//	@ResponseBody
//	public Map<String, String> getAjaxCount(HttpSession session){
//		/*通过将Map转成set就可以迭代。 map有一个方法叫做entrySet,这方法可以将Map的键值对的映射关系作为set集合的元素存储到Set集合当中，
//		 * 而这种映射关系的类型就是Entry的类型。因此可以通过使用Getkey()和getvalue()两个方法得到Set中存储的键和值。*/
//		//设置总数量
//		int count=0;
//		Map<Integer, Integer> shopCarNum=(Map<Integer, Integer>) session.getAttribute(PageConstant.SHOP_CAR);
//		if(shopCarNum!=null) {
//			//将map转换成set，并进行迭代
//			Set<Entry<Integer, Integer>> carNum=shopCarNum.entrySet();
//			for(Entry<Integer, Integer> num:carNum) {
//				count=count+num.getValue();
//			}
//		}
//		//将总数量存储到count,并将其转换成字符
//		Map<String, String> map=new HashMap<>();
//		map.put("count",count+"");
//		return map;
//		
//	}
	//对购物车进行删除操作
	@RequestMapping("/cleanCar")
	public String delShopCar(int cleanId,HttpSession session){
		
		Map<Integer, Integer> shopClean=(Map<Integer, Integer>) session.getAttribute(PageConstant.SHOP_CAR);
		//根据key清除对应的值
		shopClean.remove(cleanId);
		return "forward:showShopCar";
	}
}
